<?php
    /* 
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of product_admin
    *
    * @author Gidaff_01
    */
    class Product_admin extends Admin_Controller{

        function Product_admin() {
            parent::Admin_Controller();
            $this->load->model('Product_admin_m');
            $this->load->library('pagination');
            //$this->load->library('upload');
            $this->load->library('session');
            
            
            $this->load->helper('date');
            $this->_data['erreur'] = "";
        }
        function index() {

            $this->list_products();
        }
        function list_products(){
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/product_admin/list_products/' . '/' . $per_page . '/';
            $config['per_page'] = '5';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Product_admin_m->count_product();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Product_admin_m->get_all_products($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Product_admin_m->count_product();
            $this->_data['page_title'] = "Liste de produits";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_products');
        }
        function edit_product($id){
            $query=$this->Product_admin_m->get_product_by_id($id);
            $this->_data['query_product']=$query;
            if ($this->input->post('submit')) {

                $formVal = array(
                array(
                'field' => 'name',
                'Label' => 'Name',
                'rules' => 'required'
                ),
                array(
                'field' => 'quantity',
                'Label' => ' Quantity',
                'rules' => 'required'
                ),
                array(
                'field' => 'price',
                'Label' => 'Price',
                'rules' => 'required'
                ),
                array(
                'field' => 'poids',
                'Label' => 'Poids',
                'rules' => 'required'
                ),
                array(
                'field' => 'description',
                'Label' => 'Description',
                'rules' => 'required'
                ),
                array(
                'field' => 'promo',
                'Label' => 'Promo',
                'rules' => 'required|numeric|callback_check_promo'
                ),
          
                );
                $this->form_validation->set_rules($formVal);
                if ($this->form_validation->run()==TRUE) {
                    $image_path = './assets';
                    $thumb_path = $image_path . '/photos';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200,
                      
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        delete_pro_file($upload_info['full_path']);


                        $data = array(
                        'nomfr'=>$this->input->post('name'),
                        'catid'=>$this->input->post('cat_id'),   
                        'prix'=>$this->input->post('price'),
                        'poids'=>$this->input->post('poids'),
                        'quantite' => $this->input->post('quantity'),
                        'img' => $upload_info['file_name'] ,
                        'descriptionfr' => $this->input->post('description'),
                        'promo'  => $this->input->post('promo'), 
                        'coeur'  => $this->input->post('coeur'),  
                        );
                        $this->Product_admin_m->update_product($data,$id);
                        redirect('product_admin/list_products');
                    } else {

                        $data = array(
                        'nomfr'=>$this->input->post('name'),
                        'catid'=>$this->input->post('cat_id'),   
                        'prix'=>$this->input->post('price'),
                        'poids'=>$this->input->post('poids'), 
                        'quantite' => $this->input->post('quantity'),
                        'descriptionfr' => $this->input->post('description'),
                        'promo'  => $this->input->post('promo'), 
                        'coeur'  => $this->input->post('coeur'),  
                        );
                        $this->Product_admin_m->update_product($data,$id);
                        redirect('product_admin/list_products');
                    }
                }
                else{

                    $this->display_view('admin/edit_product');   
                }
            }
            else
            {
                $this->display_view('admin/edit_product');       
            }
        }
        function add_product(){
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'name',
                'Label' => 'Name',
                'rules' => 'required'
                ),
                array(
                'field' => 'quantity',
                'Label' => ' Quantity',
                'rules' => 'required|integer'
                ),
                array(
                'field' => 'price',
                'Label' => 'Price',
                'rules' => 'required|numeric'
                ),
                array(
                'field' => 'poids',
                'Label' => 'Poids',
                'rules' => 'required'
                ),
                array(
                'field' => 'description',
                'Label' => 'Description',
                'rules' => 'required'
                ),
                array(
                'field' => 'promo',
                'Label' => 'Promo',
                'rules' => 'required|numeric|callback_check_promo'
                ),
                );
                $this->form_validation->set_rules($formVal);
                if($this->form_validation->run()==TRUE){
                    $image_path = './assets';
                    $thumb_path = $image_path . '/photos';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload()){
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        $data = array(
                        'nomfr'=>$this->input->post('name'),
                        'catid'=>$this->input->post('cat_id'),   
                        'prix'=>$this->input->post('price'),
                         'poids'=>$this->input->post('poids'),
                        'quantite' => $this->input->post('quantity'),
                        'img' => $upload_info['file_name'] ,
                        'descriptionfr' => $this->input->post('description'),
                        'promo'  => $this->input->post('promo'), 
                        'coeur'  => $this->input->post('coeur'),  
                        );
                        $this->Product_admin_m->add_product($data);
                        redirect('product_admin/list_products');
                    }
                    else{
                        $this->display_view('admin/add_product');
                    }
                }
                else{
                    $this->display_view('admin/add_product');
                }
            }
            else{
                $this->display_view('admin/add_product');
            }
        }
        function delete_product($id,$per_page,$off_set){
            $query= $this->Product_admin_m->get_product_by_id($id);
            if($query->num_rows()>0){
                $row=$query->row();
                $file_name='./assets/photos/'.$row->img;
                if($row->img!=NULL){
                    delete_pro_file($file_name);
                }
            }
            $this->Product_admin_m->delete_product($id);
            redirect('product_admin/list_products'.'/'.$per_page.'/'.$off_set);
        }

        function list_categories(){
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/product_admin/list_categories' . '/' . $per_page . '/';
            $config['per_page'] = '5';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Product_admin_m->count_category();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Product_admin_m->get_all_categories($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Product_admin_m->count_category();
            $this->_data['page_title'] = "Liste de categories";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_categories');
        }

        function edit_category($id){
            $query=$this->Product_admin_m->get_category_by_id($id);
            $this->_data['detail_category']=$query;
            if ($this->input->post('submit')) {

                $formVal = array(
                array(
                'field' => 'name',
                'Label' => 'Name',
                'rules' => 'required'
                ),
                );
                $this->form_validation->set_rules($formVal); 
                if($this->form_validation->run()==TRUE){

                     $image_path = './assets';
                    $thumb_path = $image_path . '/photos';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200,

                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        delete_pro_file($upload_info['full_path']);


                        $data = array(
                        'nomfr'=>$this->input->post('name'),
                        'img' => $upload_info['file_name']
                        );
                        $this->Product_admin_m->update_category($data,$id);
                        redirect('product_admin/list_categories');
                    } else {

                        $data = array(
                        'nomfr'=>$this->input->post('name')
                        );
                       $this->Product_admin_m->update_category($data,$id);
                         redirect('product_admin/list_categories');
                    }

                                  }
                else{
                    $this->display_view('admin/edit_category') ;
                }  
            }
            else{
                $this->display_view('admin/edit_category') ;
            }

        }
        function delete_category($id,$per_page,$off_set)
        {
            $query= $this->Product_admin_m->get_category_by_id($id);
            if($query->num_rows()>0){
                $row=$query->row();
                $file_name='./assets/photos/'.$row->img;
                if($row->img!=NULL){
                    delete_pro_file($file_name);
                }
            }
            $this->Product_admin_m->delete_category($id);
            redirect('product_admin/list_categories'.'/'.$per_page.'/'.$off_set);  
        }

        function add_category(){
            if ($this->input->post('submit')) {

                $formVal = array(
                array(
                'field' => 'name',
                'Label' => 'Name',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal); 
                if($this->form_validation->run()==TRUE){
                    $image_path = './assets';
                    $thumb_path = $image_path . '/photos';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload()){
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        $data = array(
                         'nomfr'=>$this->input->post('name'),
                        'parent'=>$this->input->post('parent'),
                       'img' => $upload_info['file_name']
                        );
                       $this->Product_admin_m->add_category($data);
                        redirect('product_admin/list_categories');
                    }
                    else{
                        $data = array(
                         'nomfr'=>$this->input->post('name'),
                        'parent'=>$this->input->post('parent')
                        );
                       $this->Product_admin_m->add_category($data);
                        redirect('product_admin/list_categories');
                    }
                   
                }
                else{
                    $this->display_view('admin/add_category') ;
                }  
            }
            else{
                $this->display_view('admin/add_category') ;
            }

        }
        function check_promo(){
            $price=$this->input->post('price');
            $promo=$this->input->post('promo');
            if($promo>$price){
                $this->form_validation->set_message('check_promo','Promo must have smaller than the pice');
                return FALSE;
            }
            return TRUE;
        }
    }
?>
